﻿using System;

namespace PublicEntity
{
    /// <summary>
    /// FullName类
    /// </summary>
    public class FullNameHelper
    {
        static FullNameHelper()
        {
            ObjectFullName = typeof(object).FullName;
            CharFullName = typeof(char).FullName;
            IntFullName = typeof(int).FullName;
            LongFullName = typeof(long).FullName;
            StringFullName = typeof(string).FullName;
            DecimalFullName = typeof(decimal).FullName;
            DoubleFullName = typeof(double).FullName;
            FloatFullName = typeof(float).FullName;
            BoolFullName = typeof(bool).FullName;
            DateTimeFullName = typeof(DateTime).FullName;
            ByteFullName = typeof(byte).FullName;
        }

        #region 属性
        /// <summary>
        /// object 的FullName
        /// </summary>
        public static string ObjectFullName { get; private set; }

        /// <summary>
        /// char 的FullName
        /// </summary>
        public static string CharFullName { get; private set; }

        /// <summary>
        /// int 的FullName
        /// </summary>
        public static string IntFullName { get; private set; }

        /// <summary>
        /// long 的FullName
        /// </summary>
        public static string LongFullName { get; private set; }

        /// <summary>
        /// string 的FullName
        /// </summary>
        public static string StringFullName { get; private set; }

        /// <summary>
        /// decimal 的FullName
        /// </summary>
        public static string DecimalFullName { get; private set; }

        /// <summary>
        /// double 的FullName
        /// </summary>
        public static string DoubleFullName { get; private set; }

        /// <summary>
        /// float 的FullName
        /// </summary>
        public static string FloatFullName { get; private set; }

        /// <summary>
        /// bool 的FullName
        /// </summary>
        public static string BoolFullName { get; private set; }

        /// <summary>
        /// DateTime 的FullName
        /// </summary>
        public static string DateTimeFullName { get; private set; }

        /// <summary>
        /// byte 的FullName
        /// </summary>
        public static string ByteFullName { get; private set; }
        #endregion

        /// <summary>
        /// 转换value的类型
        /// </summary>
        /// <param name="type">类型Type</param>
        /// <param name="value">要转换的值</param>
        /// <returns></returns>
        public static object ObjectConvert(Type type, object value)
        {
            var val = ObjectConvert(type.FullName, value, true);
            if (val != DBNull.Value)
            {
                return val;
            }
            if (!type.BaseType.Equals(typeof(Enum)))
            {
                return value;
            }
            //表示是枚举,进行枚举转换
            if (!value.GetType().Equals(typeof(string)))
            {
                val = Convert.ToInt32(value);
            }
            else
            {
                val = value;
            }
            if (Enum.IsDefined(type, val))
            {
                return Enum.Parse(type, val.ToString(), true);
            }
            return value;
        }

        /// <summary>
        /// 转换value的类型
        /// </summary>
        /// <param name="FullName">类型Type的FullName</param>
        /// <param name="value">要转换的值</param>
        /// <param name="IsDBNull">假如没有匹配上是否转为DBNull.Value</param>
        /// <returns></returns>
        public static object ObjectConvert(string FullName, object value, bool IsDBNull = false)
        {
            if (FullName.Contains(ObjectFullName))
            {
                return value;
            }
            if (FullName.Equals(StringFullName))
            {
                return Convert.ToString(value);
            }
            if (FullName.Contains(IntFullName))
            {
                return Convert.ToInt32(value);
            }
            if (FullName.Contains(DecimalFullName))
            {
                return Convert.ToDecimal(value);
            }
            if (FullName.Contains(DoubleFullName))
            {
                return Convert.ToDouble(value);
            }
            if (FullName.Contains(BoolFullName))
            {
                return Convert.ToBoolean(value);
            }
            if (FullName.Contains(DateTimeFullName))
            {
                return Convert.ToDateTime(value);
            }
            if (FullName.Contains(LongFullName))
            {
                return Convert.ToInt64(value);
            }
            if (FullName.Contains(FloatFullName))
            {
                return Convert.ToSingle(value);
            }
            if (FullName.Contains(CharFullName))
            {
                return Convert.ToChar(value);
            }
            if (FullName.Contains(ByteFullName))
            {
                return Convert.ToByte(value);
            }
            return IsDBNull ? DBNull.Value : value;
        }
    }
}
